package weiy.top.modules.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import weiy.top.modules.brand.entity.PmsBrand;
import weiy.top.modules.brand.service.IPmsBrandService;
import weiy.top.modules.category.entity.PmsCategory;
import weiy.top.modules.category.entity.PmsCategoryBrandRelation;
import weiy.top.modules.category.mapper.PmsCategoryBrandRelationMapper;
import weiy.top.modules.category.service.IPmsCategoryBrandRelationService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import weiy.top.modules.category.service.IPmsCategoryService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: pms_category_brand_relation
 * @Author: weihancheng
 * @Date:   2021-11-24
 * @Version: V1.0
 */
@Service
public class PmsCategoryBrandRelationServiceImpl extends ServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> implements IPmsCategoryBrandRelationService {

    @Autowired
    private IPmsCategoryService pmsCategoryService;

    @Autowired
    private IPmsBrandService pmsBrandService;

    /**
     * 不仅要保存品牌和分类的关系，还要 查出来品牌名字、分类名字在保存
     *  brendName 和 CatelogName 都是冗余字段，其他表中也有
     * @param entity
     */
    @Override
    public void saveMain(PmsCategoryBrandRelation entity) {
        String brandId = entity.getBrandId();
        String catelogId = entity.getCatelogId();

        PmsBrand brand = pmsBrandService.getById(brandId);
        PmsCategory category = pmsCategoryService.getById(catelogId);

        entity.setBrandName(brand.getName());
        entity.setCatelogName(category.getName());

        this.save(entity);
    }

    @Override
    public void updateAllBrand(String brandId, String name) {
        LambdaUpdateWrapper<PmsCategoryBrandRelation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PmsCategoryBrandRelation::getBrandId, brandId);
        updateWrapper.set(PmsCategoryBrandRelation::getBrandName, name);

        this.update(updateWrapper);
    }

    @Override
    public void updateAllCategory(String cateId, String name) {
        LambdaUpdateWrapper<PmsCategoryBrandRelation> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(PmsCategoryBrandRelation::getCatelogId, cateId);
        updateWrapper.set(PmsCategoryBrandRelation::getCatelogName, name);

        this.update(updateWrapper);
    }

    @Override
    public void saveBatchMain(List<PmsCategoryBrandRelation> categoryBrandRelationList) {
        ArrayList<PmsCategoryBrandRelation> addList = new ArrayList<>();
        ArrayList<PmsCategoryBrandRelation> editList = new ArrayList<>();
        categoryBrandRelationList.forEach(entity -> {
            if (StringUtils.isEmpty(entity.getId())) {
                addList.add(entity);
            } else {
                editList.add(entity);
            }
        });
        if (addList.size() > 0) {
            this.saveBatch(addList);
        }
        if (editList.size() > 0) {
            this.updateBatchById(editList);
        }
    }

}
